package vn.intelsoft.vlogvn;

import java.util.List;

import com.actionbarsherlock.view.MenuItem;

import vn.intelsoft.vlogvn.adapter.ListAdapter;
import vn.intelsoft.vlogvn.instance.Instance;
import vn.intelsoft.vlogvn.instance.Log;
import vn.intelsoft.vlogvn.instance.Rest;
import vn.intelsoft.vlogvn.instance.Vlogger;
import vn.intelsoft.vlogvn.view.Slider;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends BaseActivity implements OnClickListener {

	private RelativeLayout layoutNotConnection, layoutLoading, layoutMain;
	private List<Vlogger> vloggers, vloggers1;
	private ListView listView;
	private Slider slider;
	private boolean isExit = false;
	private Bundle bundle;
	private Fragment mContent;

	public MainActivity() {
		super(R.string.main_activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (savedInstanceState != null)
			mContent = getSupportFragmentManager().getFragment(
					savedInstanceState, "mContent");

		setContentView(R.layout.activity_main);
		init();
	}

	private void createSlideMenu(List<Vlogger> vloggers) {
		if (mContent == null)
			mContent = new SlideMenuFragment(vloggers);
		FragmentTransaction t = this.getSupportFragmentManager()
				.beginTransaction();
		t.replace(R.id.menu_frame, mContent);
		t.commit();
	}

	private void init() {
		// TODO Auto-generated method stub
		layoutNotConnection = (RelativeLayout) MainActivity.this
				.findViewById(R.id.layout_not_connection);
		layoutMain = (RelativeLayout) MainActivity.this
				.findViewById(R.id.layout_main);
		layoutLoading = (RelativeLayout) MainActivity.this
				.findViewById(R.id.layout_loading);
		listView = (ListView) this.findViewById(R.id.list);

		getData();

	}

	private void getData() {
		layoutNotConnection.setVisibility(View.INVISIBLE);
		layoutLoading.setVisibility(View.VISIBLE);
		if (Instance.isNetworkAvailable(this)) {
			new getVloggers().execute();
		} else {
			layoutNotConnection.setVisibility(View.VISIBLE);
			layoutNotConnection.setOnClickListener(this);
		}
	}

	private class getVloggers extends AsyncTask<Void, Void, Void> {

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			layoutLoading.setVisibility(View.VISIBLE);
		}

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			String response = Rest.getJsonString(getResources().getString(R.string.xxx));
			vloggers = Rest.getVlogger(response);
			vloggers1 = Rest.getVlogger(response);
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);

			layoutLoading.setVisibility(View.INVISIBLE);
			layoutMain.setVisibility(View.VISIBLE);

			slider = new Slider(MainActivity.this, vloggers1);
			listView.addHeaderView(slider);
			ListAdapter adapter = new ListAdapter(MainActivity.this, vloggers);
			listView.setAdapter(adapter);
			createSlideMenu(vloggers);

		}

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case android.R.id.home:
			toggle();
			return true;
		case R.id.action_favorite:
			Intent it = new Intent(MainActivity.this, FavoriteActivity.class);
			MainActivity.this.startActivity(it);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if(getSlidingMenu().isMenuShowing()){
				getSlidingMenu().toggle();
			}else{
				this.onBackPressed();
			}
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		if (isExit) {
			super.onBackPressed();
			return;
		}
		this.isExit = true;
		Toast.makeText(this, "Nhấn nút Back lần nữa để thoát",
				Toast.LENGTH_SHORT).show();
		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				isExit = false;
			}
		}, 2000);
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
//		getSupportFragmentManager().putFragment(outState, "mContent", mContent);
	}

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.layout_not_connection){
			getData();
		}
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		slider.stopTask();
	}

}
